package com.LeeCode;

/**
 * 最大连续1的个数 III
 */

public class Code1004 {
    public static void main(String[] args) {
        int[] nums = {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0};
        int k = 2;
        System.out.println(new Code1004().longestOnes(nums, k));
    }

    public int longestOnes(int[] nums, int k) {
        int ans = 0, left = 0, count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0)
                count++;
            while (count > k) {
                if (nums[left] == 0)
                    count--;
                left++;
            }
            ans = Math.max(ans, i - left + 1);
        }
        return ans;
    }
}
